package com.pfsoft.grpcserver.service.impl;

import com.opencsv.CSVWriter;
import com.pfsoft.grpcserver.service.IFileOperatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author pengfei
 * @date 2022年07月19日 18:02
 */
@Service
@Slf4j
public class CsvOperatorServiceImpl implements IFileOperatorService {
    @Override
    public void generatorData(int count) {
        CSVWriter writer = null;
        try {
            String fileAbPath="C:\\matrixelements\\project\\demo\\base-record\\gradle-grpc\\grpc-server\\src\\main\\resources\\static\\test.csv";
            writer = new CSVWriter(new FileWriter(fileAbPath));

            // feed in your array (or convert your data to an array)
            for(int i=0;i<count;i++){
                List<String> colList=new ArrayList<>();
                String id = new Long(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()).toString();
                colList.add(id);
                colList.add(new Integer(i).toString());
                colList.add("22.22");
                colList.add("-11.11");
                String[] entries = colList.toArray(new String[colList.size()]);
                writer.writeNext(entries,true);
                log.info("write current line：【{}】",i);
            }
            writer.close();
        } catch (IOException e) {
            log.error("write file error",e);
        }
    }
}
